﻿using Caliburn.Micro;
using MCPlatform.Builder.WorkStation;
using MCPlatform.MachineFSM.States;
using MCPlatform.Shared.Contracts;
using MCPlatform.Shared.Enums;
using MCPlatform.ViewModels.Shell;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.MachineFSM
{
    public class FSM
    {
       // public ILogger Logger => IoC.Get<ILogger>();


        public IState? currentState;

        public Dictionary<WorkStationStatus,IState> states;

        public HomeViewModel homeViewModel;

        public MainControlStation mainControlStation;

        public FSM(HomeViewModel homeViewModel, MainControlStation mainControlStation)
        {
            this.states = new Dictionary<WorkStationStatus, IState>();
            this.homeViewModel = homeViewModel;
            this.mainControlStation = mainControlStation;
        }

        public void AddState(WorkStationStatus stateType,IState state)
        {
            if (states.ContainsKey(stateType))
            {
                return;
            }
            states.Add(stateType, state);
        }

        public void SwitchState(WorkStationStatus stateType)
        {
            if (!states.ContainsKey(stateType))
            {
               // Logger.Debug();
                return;
            }

            var newState = states[stateType];

            //先检查是否可以进入新的状态
            if (!newState.OnCheck())
            {
                return;
            }

            if (currentState!=null)
            {
                currentState.OnExit();
            }

            currentState= newState;

            currentState.OnEnter();
        }

        public void OnUpdate()
        {
            currentState?.OnUpdate();
        }


    }
}
